package com.littleHan.brushQuestions.web.frontPage.controller;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.FailedSettingException;
import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.utils.GetTokenInfoUtil;
import com.littleHan.brushQuestions.web.frontPage.service.FrontPageService;
import com.littleHan.brushQuestions.web.start.pojo.DicValue;
import com.littleHan.brushQuestions.web.start.pojo.vo.TopicVo;
import com.littleHan.brushQuestions.web.userCenter.pojo.Announcement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/frontPage")
public class frontPageController {

    @Autowired
    private FrontPageService frontPageService;

    @RequestMapping(value = "/showIndex",method = RequestMethod.GET)
    public String index(){
        return JSON.toJSONString(new Result().setCode(200).setMessage("这是登录后的主页"));
    }


    /**
     * 根据用户的兴趣(学科)获取最热的题目
     * @param request 请求对象
     * @return 是否成功获取最热题目
     */
    @RequestMapping(value = "/getHotTopicListJ",method = RequestMethod.POST)
    public String getHotTopicList(HttpServletRequest request) throws FailedToLoadException {
        String userId= GetTokenInfoUtil.getUserId(request);
        List<TopicVo> topicVoList=frontPageService.getHotTopicList(userId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取最热题目成功").setData(topicVoList));
    }

    /**
     * 根据用户的兴趣获取最新的题目
     * @param request 请求对象
     * @return 是否成功获取最新题目
     * @throws FailedToLoadException
     */
    @RequestMapping(value = "/getNewTopicListJ",method = RequestMethod.POST)
    public String getNewTopicList(HttpServletRequest request) throws FailedToLoadException {
        String userId= GetTokenInfoUtil.getUserId(request);
        List<TopicVo> topicVoList=frontPageService.getNewTopicList(userId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取最新题目成功").setData(topicVoList));
    }

    /**
     * 根据用户的id获取其兴趣
     * @param request 请求对象
     * @return 是否成功获取用户兴趣
     * @throws FailedToLoadException
     */
    @RequestMapping(value = "/getUserInterestingJ",method = RequestMethod.POST)
    public String getInteresting(HttpServletRequest request) throws FailedToLoadException{
        String userId= GetTokenInfoUtil.getUserId(request);
        List<DicValue> dicValueList=frontPageService.getInterestingSpecificInfoByUserId(userId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取用户兴趣成功").setData(dicValueList));
    }

    /**
     *获取所有兴趣
     * @return 是否成功获取所有兴趣
     * @throws FailedToLoadException
     */
    @RequestMapping(value = "/getAllObjectJ",method = RequestMethod.POST)
    public String getAllObject() throws FailedToLoadException{
        List<DicValue> dicValueList=frontPageService.getAllObject();
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取所有兴趣成功").setData(dicValueList));
    }

    /**
     * 根据用户id设置兴趣
     * @param list 兴趣列表
     * @return 是否成功修改兴趣
     */
    @RequestMapping(value = "/setInterestingByUserJ",method = RequestMethod.POST)
    public String setInterestingByUser(@RequestBody List<DicValue>list,HttpServletRequest request) throws FailedSettingException {
        String userId= GetTokenInfoUtil.getUserId(request);
        System.out.println(list);
        frontPageService.setInterestingByUser(list,userId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改兴趣成功"));

    }

    /**
     * 获取网站公告
     * @return
     */
    @GetMapping("/getAnnouncementListJ")
    public String getAnnouncementList(){
        List<Announcement> list=frontPageService.getAnnouncementList();
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取网站公告成功").setData(list));
    }

}
